<?php
namespace backend\models;

use Yii;
use yii\base\Model;
use yii\base\Exception;
use common\models\CustomerIntention;
use common\models\Employees;

class IntentionForm extends Model
{
    public $pid;
    public $uid;
    public $agreed;
    public $confusion;
    public $consider;
    public $remark;
    public $trans_user;
    public $create_user;
    public $intention;

	public function formName()
    {
        return '';
    }   

    public function scenarios()
    {
        return [
            'create' => ['pid', 'agreed', 'confusion', 'consider', 'remark', 'uid', 'trans_user'],
            'update' => ['pid', 'agreed', 'confusion', 'consider', 'remark'],
            'assigntrans' => ['trans_user', 'intention'],
        ];
    }

	public function rules()
	{
		return [
            [['pid', 'agreed', 'confusion', 'consider', 'trans_user'], 'required', 'on' => ['create', 'update'], 'message' => '不能为空'],
            ['pid', 'validateProduct', 'on' => 'create'],
            ['remark', 'safe', 'on' => ['create', 'update']],
            ['uid', 'safe', 'on' => 'create'],
            [['trans_user', 'intention'], 'required', 'on' => 'assigntrans']
		];
	}

    public function validateProduct($attribute, $params)
    {
        if (!$this->hasErrors()) {
            $customer_intention = CustomerIntention::find()->where(['cid' => $this->uid, 'pid' => $this->pid, 'status' => CustomerIntention::ACTIVE_STATUS])->one();
            if ($customer_intention) {
                $this->addError($attribute, '此产品已有意向信息');
            }
        }
    }

    /*
    * 新建用户意向
     */
    public function addIntention($uid=null)
    {
        $intention = new CustomerIntention;
        $intention->cid = $uid;
        $intention->pid = $this->pid;
        $intention->agreed = $this->agreed;
        $intention->confusion = $this->confusion;
        $intention->consider = $this->consider;
        $intention->trans_user = $this->trans_user;
        $intention->create_user = Yii::$app->user->id;
        $intention->status = CustomerIntention::ACTIVE_STATUS;
        if ($this->remark) {
           $intention->remark = $this->remark; 
        }
        
        if ($intention->save(false)) {

            return true;
        } else {

            return false;
        }
    }

    /*
    * 更新用户意向
     */
    public function updateIntention($uid=null)
    {
        $intention = CustomerIntention::find()->where(['cid' => $uid, 'pid' => $this->pid])->one();
        $intention->agreed = $this->agreed;
        $intention->confusion = $this->confusion;
        $intention->consider = $this->consider;
        if ($this->remark) {
           $intention->remark = $this->remark; 
        }
        
        if ($intention->save(false)) {

            return true;
        } else {

            return false;
        }
    }

    /*
    * 更新接待人
     */
    public function assign()
    {
        $customer_intention = CustomerIntention::findOne($this->intention);
        $customer_intention->trans_user = $this->trans_user;
        if ($customer_intention->save(false)) {

            return true;
        } else {

            return false;
        }
    }

}    